package action;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import basic.PageInfo;
import basic.QuerySet;
import basic.ResultSet;
import basic.SortInfo;
import common.BaseAction;
import common.dict.CommonDict;
import entities.TBrand;
import interfaces.IBrandService;
import net.sf.json.JSONArray;
import util.ParameterUtil;
import utils.DateUtil;
import utils.ExcelUtil;
import utils.StringUtil;

public class BrandAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7507534255609348887L;

	private IBrandService brandService;

	private TBrand brandBean;
	private List<TBrand> brandList;

	private String bndNo;
	private String bndNos;
	private String bndName;
	private String bndFactory;
	private String bndState;
	private Date bndDate;
	private Date bndDateTop;
	private Date bndDateEnd;
	private String bndRemark;

	// 展示对象
	public String show() throws Exception {
		if (!brandService.exist(bndNo)) {
			addActionError("数据不存在");
			return INFO;
		} else {
			brandBean = brandService.fetch(bndNo);
			return SUCCESS;
		}
	}

	// 编辑对象
	public String edit() throws Exception {
		if (!brandService.exist(bndNo)) {
			brandBean = new TBrand();
			brandBean.setBndNo(StringUtil.guid());
			brandBean.setBndState("1");
		} else {
			brandBean = brandService.fetch(bndNo);
		}
		return SUCCESS;
	}

	// 持久化对象
	public String save() throws Exception {
		if (!brandService.exist(bndNo)) {
			brandBean = new TBrand();
		} else {
			brandBean = brandService.fetch(bndNo);
		}
		brandBean.setBndNo(bndNo);
		brandBean.setBndName(bndName);
		brandBean.setBndFactory(bndFactory);
		brandBean.setBndState(bndState);
		brandBean.setBndDate(bndDate);
		brandBean.setBndRemark(bndRemark);

		brandService.persist(brandBean);
		return SUCCESS;
	}

	// AUTOCOMPLATE功能
	public void auto() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("bndName", bndName);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField("bndName");
		sortInfo.setSortOrder(new Boolean(true));

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TBrand> resultSet = brandService.search(querySet);
		brandList = resultSet.getResultList();

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().println(JSONArray.fromObject(brandList).toString());
	}

	// 列表展示
	// paramMap为查询条件,sortInfo为排序方式,pageInfo为分页对象
	// paramMap可为空,sortInfo可为空,pageInfo不能为空,如果需要查询全部则设置pageIndex为1,pageSize为Integer.MAX_VALUE
	public String list() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("bndNo", StringUtil.split(bndNo));
		paramMap.put("bndName", bndName);
		paramMap.put("bndFactory", bndFactory);
		paramMap.put("bndState", bndState);
		paramMap.put("bndDate", ParameterUtil.dateCombin(bndDateTop, bndDateEnd));
		paramMap.put("bndRemark", bndRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "bndDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(pageIndex, pageSize));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TBrand> resultSet = brandService.search(querySet);
		brandList = resultSet.getResultList();
		pageInfo = resultSet.getPageInfo();
		return SUCCESS;
	}

	// 导出数据 查询条件等同于列表查询条件
	public String export() throws Exception {
		QuerySet<Object> querySet = new QuerySet<Object>();

		HashMap<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("bndNo", StringUtil.split(bndNo));
		paramMap.put("bndName", bndName);
		paramMap.put("bndFactory", bndFactory);
		paramMap.put("bndState", bndState);
		paramMap.put("bndDate", ParameterUtil.dateCombin(bndDateTop, bndDateEnd));
		paramMap.put("bndRemark", bndRemark);

		SortInfo sortInfo = new SortInfo();
		sortInfo.setSortField((StringUtil.isEmpty(sortField)) ? "bndDate" : sortField);
		sortInfo.setSortOrder(sortOrder);

		querySet.setPageInfo(new PageInfo(1, CommonDict.EXPORT_LIMIT));
		querySet.setSortInfo(sortInfo);
		querySet.setParamMap(paramMap);

		ResultSet<TBrand> resultSet = brandService.search(querySet);
		brandList = resultSet.getResultList();

		List<String> fieldHead = new ArrayList<String>();
		fieldHead.add("编号");
		fieldHead.add("品牌名称");
		fieldHead.add("厂商");
		fieldHead.add("是否可用");
		fieldHead.add("日期日期");
		fieldHead.add("日期时间");
		fieldHead.add("备注");

		List<List<String>> fieldData = new ArrayList<List<String>>();

		for (TBrand brandBean : brandList) {
			List<String> fieldItem = new ArrayList<String>();
			fieldItem.add(brandBean.getBndNo());
			fieldItem.add(brandBean.getBndName());
			fieldItem.add(brandBean.getBndFactory());
			fieldItem.add((brandBean.getBndState().equals("0")) ? "无效" : "有效");
			fieldItem.add(DateUtil.format(brandBean.getBndDate(), DateUtil.DATE));
			fieldItem.add(DateUtil.format(brandBean.getBndDate(), DateUtil.TIME));
			fieldItem.add(brandBean.getBndRemark());

			fieldData.add(fieldItem);
		}

		inputStream = ExcelUtil.excel(fieldHead, fieldData);
		return SUCCESS;
	}

	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////
	////////////////////// ---------------------------//////////////////////////////////////////////////////////////////////////////////////////////////////////////

	public IBrandService getBrandService() {
		return brandService;
	}

	public void setBrandService(IBrandService brandService) {
		this.brandService = brandService;
	}

	public TBrand getBrandBean() {
		return brandBean;
	}

	public void setBrandBean(TBrand brandBean) {
		this.brandBean = brandBean;
	}

	public List<TBrand> getBrandList() {
		return brandList;
	}

	public void setBrandList(List<TBrand> brandList) {
		this.brandList = brandList;
	}

	public String getBndNo() {
		return bndNo;
	}

	public void setBndNo(String bndNo) {
		this.bndNo = bndNo;
	}

	public String getBndNos() {
		return bndNos;
	}

	public void setBndNos(String bndNos) {
		this.bndNos = bndNos;
	}

	public String getBndName() {
		return bndName;
	}

	public void setBndName(String bndName) {
		this.bndName = bndName;
	}

	public String getBndFactory() {
		return bndFactory;
	}

	public void setBndFactory(String bndFactory) {
		this.bndFactory = bndFactory;
	}

	public String getBndState() {
		return bndState;
	}

	public void setBndState(String bndState) {
		this.bndState = bndState;
	}

	public Date getBndDate() {
		return bndDate;
	}

	public void setBndDate(Date bndDate) {
		this.bndDate = bndDate;
	}

	public Date getBndDateTop() {
		return bndDateTop;
	}

	public void setBndDateTop(Date bndDateTop) {
		this.bndDateTop = bndDateTop;
	}

	public Date getBndDateEnd() {
		return bndDateEnd;
	}

	public void setBndDateEnd(Date bndDateEnd) {
		this.bndDateEnd = bndDateEnd;
	}

	public String getBndRemark() {
		return bndRemark;
	}

	public void setBndRemark(String bndRemark) {
		this.bndRemark = bndRemark;
	}

}
